﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ScoreMS.Controls
{
    /// <summary>
    /// NumericUpDown.xaml 的交互逻辑
    /// </summary>
    public partial class NumericUpDown : UserControl, INotifyPropertyChanged    //莫忘 INotifyPropertyChanged
    {
        private int numericValue = 1;

        public NumericUpDown()
		{
			this.InitializeComponent();
            this.DataContext = this;
		}

        public delegate void ValueChangedEventHandler(object sender, EventArgs e);
        public event ValueChangedEventHandler ValueChangedEvent;
        void RaiseValueChanged()
        {
            if (ValueChangedEvent != null)
            {
                ValueChangedEvent(this, EventArgs.Empty);
            }
        }

        public int Value
        {
            get { return numericValue; }
            set
            {
                if (numericValue != value)
                {
                    numericValue = value;
                    NotifyPropertyChanged("Value");
                    RaiseValueChanged();    //触发 ValueChangedEvent
                }
            }
        }
        public int Increment { get; set; }
        public int MaxValue { get; set; }
        public int MinValue { get; set; }

        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            int newValue = (Value + Increment);
            if (newValue < MinValue)
            {
                Value = MinValue;
            }
            else if (newValue > MaxValue)
            {
                Value = MaxValue;
            }
            else
            {
                Value = newValue;
            }
            //updateText(); 
        }

        private void DownButton_Click(object sender, RoutedEventArgs e)
        {
            int newValue = (Value - Increment);
            if (newValue < MinValue)
            {
                Value = MinValue;
            }
            else if (newValue > MaxValue)
            {
                Value = MaxValue;
            }
            else
            {
                Value = newValue;
            }
            //updateText();  
        }

        private void ValueText_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                Value = int.Parse(ValueText.Text);
            }
            catch (Exception)
            {
                Value = 0;
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}